package midianet.digital.modelo.persistencia;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import midianet.digital.excessao.InfraExcessao;
import midianet.digital.modelo.entidade.Uf;

import org.springframework.stereotype.Repository;

@Repository
public class UfDAO {
	
	@PersistenceContext
	protected EntityManager persistencia;
	
	@SuppressWarnings("unchecked")
	public List<Uf> listar(final String query)throws InfraExcessao{
		List<Uf> retorno = null;
		
		try{
			final Query consulta =  persistencia.createNamedQuery(query);
			retorno = consulta.getResultList();
		}catch(final Exception e){
			throw new InfraExcessao(e);
		}
		
		return retorno;
	}
	
	public Uf obterPorId(String id) throws InfraExcessao{
		Uf retorno = null;
		
		try{
			retorno = persistencia.find(Uf.class, id);
		}catch(final NoResultException e){
		
		}catch(final Exception e){
			throw new InfraExcessao(e);
		}
		
		return retorno;
	}
	
	public void salvar(Uf uf) throws InfraExcessao{
		
		try{
			persistencia.persist(uf);
		}catch(final Exception e){
			throw new InfraExcessao(e);
		}
	}
	
}